package com.example.zhihuhelpapp.activity;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.adapter.OrderListAdapter;
import com.example.zhihuhelpapp.base.MyBaseRxActivity;
import com.example.zhihuhelpapp.bean.JDOrderResult;
import com.example.zhihuhelpapp.mvp.JingDongApiPresenter;
import com.example.zhihuhelpapp.mvp.contract.IJongDongApiContract;
import com.example.zhihuhelpapp.utils.FormatUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class OrderListActivity extends MyBaseRxActivity<JingDongApiPresenter>
        implements IJongDongApiContract.IView, View.OnClickListener {
    private List<JDOrderResult.JdUnionOpenOrderRowQueryResponseBean.JDOrderBean.DataBean> mDdtas;
    private OrderListAdapter mAdapter;

    private int lastHourCount = 0;
    private TextView mTvTotal;
    private boolean isGetAll = false;

    int orderCount = 0;

    @Override
    protected JingDongApiPresenter createPresenter() {
        return new JingDongApiPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void initView() {
        showToolBar(true, getString(R.string.title_jd_order_list));

        findViewById(R.id.tv_no_order_tip).setOnClickListener(this);
        findViewById(R.id.tv_get_today).setOnClickListener(this);
        findViewById(R.id.tv_get_valid_order).setOnClickListener(this);
        mTvTotal = findViewById(R.id.mTvTotal);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mDdtas = new ArrayList<>();
        mAdapter = new OrderListAdapter(this, mDdtas);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getOrderList();
    }

    private void getOrderList() {
        orderCount = 0;
        long time = System.currentTimeMillis() - lastHourCount * FormatUtils.anHour;
        showProgress();
        mPresenter.getOrderList(time);
    }

    private void getTodayOrderList() {
        orderCount = 0;
        mDdtas.clear();
        mAdapter.notifyDataSetChanged();
        isGetAll = true;

        long todayStartTime = FormatUtils.getStartTime().getTime() + FormatUtils.anHour;
        long todayEndTime = System.currentTimeMillis() + FormatUtils.anHour;
        while (todayStartTime < todayEndTime) {
            mPresenter.getOrderList(todayStartTime);
            todayStartTime += FormatUtils.anHour;
        }
    }

    @Override
    public void onGetOrderList(List<JDOrderResult.JdUnionOpenOrderRowQueryResponseBean.JDOrderBean.DataBean> orderList) {
        if (orderList != null && orderList.size() > 0) {
            mDdtas.addAll(orderList);

            Collections.sort(mDdtas, (t0, t1) -> {
                long t0Time = FormatUtils.strToLong(t0.getOrderTime(), FormatUtils.format_);
                long t1Time = FormatUtils.strToLong(t1.getOrderTime(), FormatUtils.format_);
                if (t0Time == t1Time) {
                    return 0;
                } else if (t0Time < t1Time) {
                    return 1;
                } else {
                    return -1;
                }
            });

            mAdapter.notifyDataSetChanged();

            double total = 0.0f;
            double gmvTotal = 0.0f;
            long lastOrderId = 0L;
            for (JDOrderResult.JdUnionOpenOrderRowQueryResponseBean.JDOrderBean.DataBean mDdta : mDdtas) {
                if (mDdta.isValidOrder()) {
                    total = BigDecimal.valueOf(total).add(BigDecimal.valueOf(mDdta.getEstimateFeeAct())).doubleValue();
                    gmvTotal = BigDecimal.valueOf(gmvTotal).add(BigDecimal.valueOf(mDdta.getEstimateCosPrice())).doubleValue();

                    if (mDdta.getEstimateFee() > 0) {
                        if (lastOrderId == mDdta.getParentId()) {
                            continue;
                        }
                        lastOrderId = mDdta.getParentId();
                        orderCount += 1;
                    }
                }
            }

            if (isGetAll) {
                mTvTotal.setText("当日佣金金总额：" + total);
            } else {
                mTvTotal.setText("最近一小时佣金总额：" + total);
            }

            mTvTotal.append(" 有效订单个数：" + mDdtas.size());
            mTvTotal.append(" 订单GMV：" + gmvTotal);
        } else if (mDdtas.size() == 0) {
            mTvTotal.setText("没有订单信息");
        }
    }

    @Override
    public void onNoOrder() {
        if (mDdtas.size() == 0) {
            mTvTotal.setText("没有订单信息");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_no_order_tip) {
            mDdtas.clear();
            mAdapter.notifyDataSetChanged();
            isGetAll = false;
            lastHourCount += 1;
            getOrderList();
        } else if (v.getId() == R.id.tv_get_today) {
            getTodayOrderList();
        } else if (v.getId() == R.id.tv_get_valid_order) {
            showValidOrder();
        }
    }

    private void showValidOrder() {
        if (mDdtas.size() > 0) {
            double total = 0.0f;
            double gmvTotal = 0.0f;
            List<String> orderTimeList = new ArrayList<>();
            List<JDOrderResult.JdUnionOpenOrderRowQueryResponseBean.JDOrderBean.DataBean> validOrder = new ArrayList<>();
            for (JDOrderResult.JdUnionOpenOrderRowQueryResponseBean.JDOrderBean.DataBean mDdta : mDdtas) {
                if (mDdta.isValidOrder() && mDdta.getEstimateCosPrice() > 0) {
                    if (!orderTimeList.contains(mDdta.getOrderTime())) {
                        orderTimeList.add(mDdta.getOrderTime());
                    }
                    validOrder.add(mDdta);
                    total = BigDecimal.valueOf(total).add(BigDecimal.valueOf(mDdta.getEstimateFeeAct())).doubleValue();
                    gmvTotal = BigDecimal.valueOf(gmvTotal).add(BigDecimal.valueOf(mDdta.getEstimateCosPrice())).doubleValue();
                }
            }
            mDdtas.clear();
            mDdtas.addAll(validOrder);
            mAdapter.notifyDataSetChanged();


            mTvTotal.setText("当日佣金金总额：" + total);
            mTvTotal.append(" 有效订单个数：" + orderTimeList.size());
            mTvTotal.append(" 订单GMV：" + gmvTotal);
        }
    }
}
